package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.MemberInfoResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

/**
 * @Author      : hxw
 * @Date        : 2023/3/2 10:41
 * @Describe    :
 */
class MemberInfoViewModel : BaseViewModel() {

    var etSearchContent = StringObservableField()

    private var pageSize: Int = 20
    private var pageNo: Int = 1

    /** 退出登录 */
    val logOutResult: MutableLiveData<UiState> = MutableLiveData()

    /**  获取组织方下会员列表  */
    var memberInfoResult: MutableLiveData<ListDataUiState<MemberInfoResponse.ListData>> = MutableLiveData()
    fun getMemberList(isRefresh: Boolean, shedNo: String, entryName: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getMemberList(pageNo, pageSize, shedNo, entryName) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            memberInfoResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<MemberInfoResponse.ListData>()
            )
            memberInfoResult.value = listDataUiState
        })
    }

    /** 退出登录 */
    fun loginOut() {
        request({ apiService.loginOut() }, {
            val deleteState = UiState(isSuccess = true)
            logOutResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            logOutResult.value = deleteState
        })
    }
}